#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
ll fib[N];
ll f(ll x) {
  ll y = *(lower_bound(fib + 1, fib + 60, x) - 1);
  return x - y;
}
ll lca(ll u, ll v) {
  while (u != v) {
    if (u < v) swap(u, v);
    u = f(u);
  }
  return u;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  fib[0] = fib[1] = 1;
  rep(i, 2, 59) fib[i] = fib[i - 1] + fib[i - 2];
  cin >> n;
  while (n--) {
    ll x, y;
    cin >> x >> y;
    cout << lca(x, y) << endl;
  }
  return 0;
}